<?php

/**
 * Authentication helper class
 *
 * @author 
 */
class EICms_Auth extends Zend_Auth {

	/**
	 * Instance container
	 *
	 * @var EICms_Auth
	 */
	protected static $_instance;
	
	/**
	 * Constructor stub.
	 *
	 */
	protected function __construct() {
		
	}
	
	/**
	 * Singleton returns the Auth instance.
	 *
	 * @return EICms_Auth
	 */
	public static function getInstance() {
		if(empty(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * Returns the identity information.
	 *
	 * @return object
	 */
	public function getIdentity() {
		if(!$this->hasIdentity()) {
			$adapter = new Zend_Auth_Adapter_DbTable(EICms_Table::getDefaultAdapter(),'user','name','password');
			$adapter->setIdentity('guest');
			$adapter->setCredential(md5('guest'));
			
			$result = parent::authenticate($adapter);
			if(!$result->isValid()) {
				throw new EICms_Auth_Exception('No guest user found. Please contact the administrator');
			}
			$guest = $adapter->getResultRowObject(null,'password');
			$guest->group = 'guest';
			$this->getStorage()->write($guest);
		}
		return parent::getIdentity();
	}

	/**
	 * Checks if the current identity is a guest.
	 *
	 * @return bool
	 */
	public function isGuest() {
		if(!$this->hasIdentity() || $this->getIdentity()->name == 'guest') {
			return true;
		}
		return false;
	}
	
	/**
	 * Performs the login operations.
	 *
	 * @param string $name
	 * @param string $password
	 * @return Zend_Auth_Result
	 */
	public function login($name,$password) {
		$adapter = new Zend_Auth_Adapter_DbTable(
					Zend_Db_Table::getDefaultAdapter(),
					'user','name','password');
		$adapter->setIdentity($name)
					->setCredential(md5($password));
		
		$result = $this->authenticate($adapter);
		
		if(!$result->isValid()) {
			return $result;
		}
		else {
			$user = $adapter->getResultRowObject(null,'password');
			$group = new EICms_Table_Group();
			$userGroup = $group->find($user->group_id)->current();
			$user->group = $userGroup->name;
			
			$session = new EICms_Table_UserSession();
			$recSession = $session->createRow();
			$recSession->user_id = $user->id;
			$recSession->login_ip = $_SERVER['REMOTE_ADDR'];
			$recSession->user_agent = $_SERVER['HTTP_USER_AGENT'];
			$recSession->user_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
			$recSession->php_session = Zend_Session::getId();
			$recSession->datetime_login = date('Y-m-d H:i:S');
			$recSession->group_id = $user->group_id;
			
			$this->getStorage()->write($user);
			
			$recSession->save();
			
			$user->recent_session = $recSession->id;
			
			$userObj = new EICms_Table_User();
			$currUsr = $userObj->find($user->id)->current();
			$currUsr->recent_session = $recSession->id;
			$currUsr->save();
		}
		return $result;
	}
	
	/**
	 * Performs logout operations.
	 *
	 * @return void
	 */
	public function logout() {
		$session = new EICms_Table_UserSession();
		$currSession = $session->find($this->getIdentity()->recent_session)->current();
		$currSession->datetime_logout = date('Y-m-d H:i:s');
		$currSession->save();
		
		$this->clearIdentity();
	}
}
?>